﻿using System;

using Fuse8.DomainFramework.Common;
using Fuse8.DomainFramework.Common.Locator;
using Fuse8.DomainFramework.BusinessService;

using Fuse8.ContentManagementFramework.Domain;
using Fuse8.ContentManagementFramework.Domain.Commands;
using Fuse8.ContentManagementFramework.Domain.Commands.CommandParameters;

namespace Fuse8.ContentManagementFramework.BizFacade.Services
{
    public class CmsPageControlService : CmsBusinessService<CmsPageControl>
    {
        public virtual void AddPageContent(int pageId, CmsPageControl control, bool isWorkflowEnabled)
        {
            var command = DependencyServiceLocator.Current.GetInstance<CmsPageAddControlCommand>();

            var commandParameter = new CmsPageAddControlCommandParameter
            {
                PageId = pageId,
                TemplateRegionId = control.TemplateRegionId,
                PageControlContent = control.PageControlContent,
                PageControlTypeKey = (int)PageControlTypeKind.Content,
                IsLatestCompletedRevision = !isWorkflowEnabled,
                IsPublished = !isWorkflowEnabled,
                PageControlOrder = control.PageControlOrder,
                AdminCaption = control.AdminCaption
            };

            TrackingEntityObjectMediator.TrackEntity(commandParameter);
            SiteAwareEntityObjectMediator.SetSiteEntity(commandParameter);

            command.ConfigureCommand(commandParameter);

            Execute(command);
        }

        /// <summary>
        /// Adds linked content (snippet) as page control
        /// </summary>
        /// <param name="pageId">Updating page id</param>
        /// <param name="control">Control to add</param>
        /// <param name="isWorkflowEnabled">Indicator whether workflow is enabled or not</param>        
        public void AddPageSnippet(int pageId, CmsPageControl control, bool isWorkflowEnabled)
        {
            var command = DependencyServiceLocator.Current.GetInstance<CmsPageAddControlCommand>();

            var commandParameter = new CmsPageAddControlCommandParameter
            {
                PageId = pageId,
                TemplateRegionId = control.TemplateRegionId,
                ContentId = control.PageControlContentId,
                PageControlTypeKey = (int)PageControlTypeKind.Snippet,
                IsLatestCompletedRevision = !isWorkflowEnabled,
                IsPublished = !isWorkflowEnabled,
                PageControlOrder = control.PageControlOrder,
                AdminCaption = control.AdminCaption
            };

            TrackingEntityObjectMediator.TrackEntity(commandParameter);
            SiteAwareEntityObjectMediator.SetSiteEntity(commandParameter);

            command.ConfigureCommand(commandParameter);

            Execute(command);
        }

        public virtual void AddPagePartialControl(int pageId, int templateRegionId, string viewValue, string modelValue, bool isWorkflowEnabled, int pageControlOrder, PageControlTypeKind type)
        {
            var command = DependencyServiceLocator.Current.GetInstance<CmsPageAddControlCommand>();

            var commandParameter = new CmsPageAddControlCommandParameter
            {
                PageId = pageId,
                TemplateRegionId = templateRegionId,
                PageControlTypeKey = (int)type,
                PageControlViewValue = viewValue,
                PageControlModelValue = modelValue,
                IsLatestCompletedRevision = !isWorkflowEnabled,
                IsPublished = !isWorkflowEnabled,
                PageControlOrder = pageControlOrder
            };

            TrackingEntityObjectMediator.TrackEntity(commandParameter);
            SiteAwareEntityObjectMediator.SetSiteEntity(commandParameter);

            command.ConfigureCommand(commandParameter);

            Execute(command);
        }

        public virtual void AddModule(int pageId, CmsPageControl control, bool isWorkflowEnabled)
        {
            var command = DependencyServiceLocator.Current.GetInstance<CmsPageAddControlCommand>();

            var commandParameter = new CmsPageAddControlCommandParameter
            {
                PageId = pageId,
                TemplateRegionId = control.TemplateRegionId,
                PageControlTypeKey = (int)PageControlTypeKind.ModulePart,
                ModuleId = control.ModuleId,
                PageControlModelValue = control.PageControlModelValue,
                IsLatestCompletedRevision = !isWorkflowEnabled,
                IsPublished = !isWorkflowEnabled,
                PageControlOrder = control.PageControlOrder,
                AdminCaption = control.AdminCaption
            };

            TrackingEntityObjectMediator.TrackEntity(commandParameter);
            SiteAwareEntityObjectMediator.SetSiteEntity(commandParameter);

            command.ConfigureCommand(commandParameter);

            Execute(command);
        }
        
        public override void Save(CmsPageControl entityObject)
        {
            Guard.ArgumentNotNull(entityObject, "entityObject");

            var command = DependencyServiceLocator.Current.GetInstance<CmsUpdatePageControlCommand>();

            var commandParameter = new CmsUpdatePageControlCommandParameter
            {
                Id = entityObject.Id,
                PageControlContentId = entityObject.PageControlContentId,
                ModuleId = entityObject.ModuleId,
                PageControlContent = entityObject.PageControlContent,
                PageControlViewValue = entityObject.PageControlViewValue,
                PageControlModelValue = entityObject.PageControlModelValue,
                PageControlOrder = entityObject.PageControlOrder,
                AdminCaption = entityObject.AdminCaption
            };

            TrackingEntityObjectMediator.TrackEntity(commandParameter);
            SiteAwareEntityObjectMediator.SetSiteEntity(commandParameter);

            command.ConfigureCommand(commandParameter);

            Execute(command);
        }

        public void Move(int pageControlId, bool isMoveUp)
        {
            var command = DependencyServiceLocator.Current.GetInstance<CmsPageControlMoveCommand>();

            var commandParameter = new CmsPageControlMoveCommandParameter
            {
                PageControlId = pageControlId,
                IsMoveUp = isMoveUp
            };

            command.ConfigureCommand(commandParameter);

            using (var transaction = new BusinessServiceTransaction<CmsPageControl>())
            {
                Execute(command);

                transaction.Commit();
            }
        }

        public void ChangeTemplateRegions(int previousPageId, int latestPageId)
        {
            var command = DependencyServiceLocator.Current.GetInstance<CmsPageControlsChangeTemplateRegionsCommand>();

            var commandParameter = new CmsPageControlsChangeTemplateRegionsCommandParameter()
            {
                PreviousPageId = previousPageId,
                LatestPageId = latestPageId
            };

            command.ConfigureCommand(commandParameter);

            Execute(command);
        }
    }
}
